package com.zmn.plat.business.impl.company;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto.VtDTO;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.plat.business.interfaces.company.BaseCompanyAreaBService;
import com.zmn.plat.common.constant.BaseAreaConsts;
import com.zmn.plat.common.exception.PlatException;
import com.zmn.plat.model.entity.area.BaseArea;
import com.zmn.plat.model.entity.company.BaseCompanyArea;
import com.zmn.plat.services.interfaces.area.BaseAreaService;
import com.zmn.plat.services.interfaces.company.BaseCompanyAreaService;
import com.zmn.plat.services.interfaces.company.BaseCompanyService;
import com.zmn.plat.services.interfaces.product.ServProductRelationService;
import com.zmn.plat.services.interfaces.product.ServProductService;
import com.zmn.sp.common.enums.available.AvailableAreaLevelEnum;
import com.zmn.sp.dubbo.interfaces.available.AvailableAreaListRemoteService;
import com.zmn.sp.dubbo.utils.DubboConsts;
import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @author tanbiao
 * @description
 * @since 2020/2/27 15:55
 * @modifier
 **/
@Service
public class BaseCompanyAreaBServiceImpl implements BaseCompanyAreaBService {
    private Logger logger = LoggerFactory.getLogger(BaseCompanyAreaBServiceImpl.class);
    @Resource
    BaseCompanyAreaService baseCompanyAreaService;
    @Resource
    BaseAreaService baseAreaService;
    @Resource
    BaseCompanyService baseCompanyService;
    @Resource
    ServProductRelationService servProductRelationService;
    @Resource
    ServProductService servProductService;

    @Reference(version = DubboConsts.INTERFACE_VERSION,check = false)
    private AvailableAreaListRemoteService availableAreaListRemoteService;
    @Override
    public List<VtDTO> listCompanyByAreaId(Integer areaId, Integer plat) {
        BaseArea baseArea = baseAreaService.findByKey(areaId);
        BaseCompanyArea query = new BaseCompanyArea();
        query.setPlat(plat);
        query.setStatus(GlobalConsts.YES);
        AvailableAreaLevelEnum level = null;
        if (Objects.equals(baseArea.getLevel(), BaseAreaConsts.AREA_LEVEL_TWO)) {
            level= AvailableAreaLevelEnum.PROVINCE;
        }
        if (Objects.equals(baseArea.getLevel(), BaseAreaConsts.AREA_LEVEL_THREE)) {
            level = AvailableAreaLevelEnum.CITY;
        }
        if (Objects.equals(baseArea.getLevel(), BaseAreaConsts.AREA_LEVEL_FOUR)) {
            level = AvailableAreaLevelEnum.COUNTY;
        }
        ResponseDTO<List<VtDTO>> responseDTO = availableAreaListRemoteService.listSpByAreaIdSetAndLevel(Collections.singleton(areaId), level, plat);
        if (!responseDTO.isSuccess()) {
            throw new PlatException("无法根据区域取得服务商");
        }
        return Optional.ofNullable(responseDTO.getData()).orElse(Collections.emptyList());
    }

}
